package com.keyinong.caizhengting.tools;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore.Images;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class ImgChangeUtil {
    // @Override
    // protected void onActivityResult(int requestCode, int resultCode, Intent
    // data) {
    // if (requestCode == 1) {
    // if (resultCode == Activity.RESULT_CANCELED)
    // return;
    // Bundle bundle = data.getExtras();
    // if (bundle != null) {
    // Bitmap bitmap = bundle.getParcelable("data");
    // Uri uri = saveBitmap(bitmap);
    // startPhotoZoom(uri);
    // }
    // // } else if (requestCode == ALBUM_REQUEST_CODE) {
    // // if (resultCode == Activity.RESULT_CANCELED)
    // // return;
    // // Uri uri = data.getData();
    // // Uri fileUri = converUri(uri);
    // // startImageZoom(fileUri);
    // } else if (requestCode == 3) {
    // Bundle bundle = data.getExtras();
    // Bitmap bitmap = bundle.getParcelable("data");
    // img_roominfo.setImageBitmap(bitmap);
    // // sendImage(bitmap);
    // }
    // }

    /**
     * 将Content的Uri转成file的Uri
     *
     * @param uri
     * @return
     */
    public static Uri converUri(Context context, Uri uri) {
        InputStream in = null;
        try {
            in = context.getContentResolver().openInputStream(uri);
            Bitmap bitmap = BitmapFactory.decodeStream(in);
            return saveBitmap(bitmap);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 将Bitmap转成file的Uri
     *
     * @param bitmap
     * @return
     */
    public static Uri saveBitmap(Bitmap bitmap) {
        File file = new File(Environment.getExternalStorageDirectory() + "/aaa");
        if (!file.exists())
            file.mkdirs();
        File imgFile = new File(file.getAbsolutePath() + "/1.jpeg");
        if (imgFile.exists())
            imgFile.delete();
        try {
            FileOutputStream outputStream = new FileOutputStream(imgFile);
            bitmap.compress(CompressFormat.JPEG, 85, outputStream);
            outputStream.flush();
            outputStream.close();
            Uri uri = Uri.fromFile(imgFile);
            return uri;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Uri geturi(Context context, android.content.Intent intent) {
        Uri uri = intent.getData();
        String type = intent.getType();
        if (uri.getScheme().equals("file") && (type.contains("image/"))) {
            String path = uri.getEncodedPath();
            if (path != null) {
                path = Uri.decode(path);
                ContentResolver cr = context.getContentResolver();
                StringBuffer buff = new StringBuffer();
                buff.append("(").append(Images.ImageColumns.DATA).append("=")
                        .append("'" + path + "'").append(")");
                Cursor cur = cr.query(Images.Media.EXTERNAL_CONTENT_URI,
                        new String[]{Images.ImageColumns._ID},
                        buff.toString(), null, null);
                int index = 0;
                for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
                    index = cur.getColumnIndex(Images.ImageColumns._ID);
                    // set _id value
                    index = cur.getInt(index);
                }
                if (index == 0) {
                    // do nothing
                } else {
                    Uri uri_temp = Uri
                            .parse("content://media/external/images/media/"
                                    + index);
                    if (uri_temp != null) {
                        uri = uri_temp;
                    }
                }
            }
        }
        return uri;
    }

}
